#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@NAME          : genttfhtml.py
@TIME          : 2024/11/21 18:55:16
@AUTHOR        : chenlip
@VERSION       : 0.0.1
@DESCRIPTION   : 小红书爬虫的Cookie操作
'''
# --------------------------引用--------------------------

import json
from Config import RedBookConf as Config

class RedCookie(object):

    def __init__(self) -> None:
        pass

    def set_cookie():
        """设置Cookie"""
        try:
            for cookie in json.loads(Config.CookiesDict[Config.CurrentUser]):
                Config.Browser.add_cookie(cookie)
            Config.Browser.refresh()
        except KeyError:
            return


    def get_new_cookie():
        """获取新的Cookie"""
        cookies = json.dumps(Config.Browser.get_cookies())
        Config.CookiesDict[Config.CurrentUser] = cookies
        RedCookie.set_cookie()


    def save_cookie():
        """保存Cookie"""
        print("正在保存Cookie")
        with open('cookies.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(Config.CookiesDict))
        print('cookies保存成功！')
